/*
 * Axis.h
 *
 *  Created on: Mar 10, 2011
 *      Author: Jason
 */

#ifndef AXIS_H_
#define AXIS_H_

#include "pin.h"
#include "stepper.h"
#include "stddef.h"
#include "timer0.h"
#include <math.h>

enum positionRef {ABSOLUTE, RELATIVE};
	
class axis
{
public:
	axis(void);
	
	//setting up the min and max travel inputs	
	void setupMin(byte, byte);
	void setupMax(byte, byte);

	//invert the min and max pins if needed
	void invertMin(state);
	void invertMax(state);

	//set the maximum travel
	void setMaxSteps(unsigned long);
	unsigned long getMaxSteps(void);

	//update the axis
	byte update(void);

	//zero out the axis at velocity f and s
	void zero(double, double);

	//set the axis to travel to a target, with a velocity, and a rate of acceleration
	byte go(double, double, double);
	byte goA(double, double, double);
	byte goR(double, double, double);
	
	//call go and update the axis until it is in position
	void move(double);

	state getMin(state);
	state getMax(state);

	double getPosition(void);
	void setPosition(double);

	void setVel(double);
	double getVel(void);

	void setAcc(double);
	double getAcc(void);

	void setVelMax(double);	
	unsigned long time(void);

	byte busy(void);
	
	void setRef(positionRef);
	positionRef getRef(void);

	stepper drive;

	pin 	_min; //minimum travel input
	pin 	_max; //maximum travel input

	//virtual ~axis();

private:


	double		 	_position;  //axis current position
	double		 	_target;	//axis target position
	unsigned long	_delay;		//calculated delay between pulses
	unsigned long 	_timestamp; //time stamp for the next pulse
	unsigned long 	_steps;		//number of steps remaining 
	unsigned long	_aStepsL;	//number of steps for the acceleration period
	unsigned long	_aStepsH;	//number of steps for the acceleration period
	unsigned long 	_maxSteps;	//the maximum position

	double _vel;	//velocity: steps per second
	double _velMax; //Maximum allowable velocity for an axis
	double _acc;	//Acceleration: steps per second^2
	
	positionRef		_ref;

};

#endif /* AXIS_H_ */
